var Login = function () {
    
    return {
        //main function to initiate the module
        init: function () {
        	var loginProcess = function(){
				$.ajax({
					url: $("#form-login").attr("action"),
					data: $("#form-login").serialize(),
					beforeSend: function(response){
						$("#submit").html('Loading <i class="m-icon-swapright m-icon-white"></i>');
						var el = jQuery(".content");
						App.blockUI(el);
					},
					success: function(respon) {
						var el = jQuery(".content");
						App.unblockUI(el);
						
						$("#submit").html('Login <i class="m-icon-swapright m-icon-white"></i>');
						
						if(respon == ""){
							
							$("#error span").text("Error : Tidak ada respon dari server!");
							
							$("#error").show();
							
							return false;
						}
						try {
							var obj = jQuery.parseJSON(respon);
						} catch (e) {
							// error
							
							$("#error span").text("Error : Parsing JSON failed!");
							
							$("#error").show();
							
							return false;
						}
						
						if(obj.status == "success"){
							window.location = obj.redirect;
						}else if(obj.status == "error"){
							$("#error span").text(obj.msg);
							
							$("#error").show();
							
							return false;
						}

						return false;
					},
					error: function(jqXHR,error, errorThrown) {
						var el = jQuery(".content");
						App.unblockUI(el);
						$("#submit").html('Login <i class="m-icon-swapright m-icon-white"></i>');
						a = error+" : "+jqXHR.status+" "+errorThrown+"!"+"<br />"+jqXHR.responseText;
						
						$("#error span").html(obj.msg);
						
						$("#error").show();
						
						return false;
					},
					type: "post", 
					dataType: "html"
				});
			}
			
           $('.login-form').validate({
	            errorElement: 'label', //default input error message container
	            errorClass: 'help-inline', // default input error message class
	            focusInvalid: false, // do not focus the last invalid input
	            rules: {
	                username: {
	                    required: true
	                },
	                password: {
	                    required: true
	                },
	                remember: {
	                    required: false
	                }
	            },

	            messages: {
	                username: {
	                    required: "Username / Email is required."
	                },
	                password: {
	                    required: "Password is required."
	                }
	            },

	            invalidHandler: function (event, validator) { //display error alert on form submit   
	                $('.alert-error', $('.login-form')).show();
	            },

	            highlight: function (element) { // hightlight error inputs
	                $(element)
	                    .closest('.control-group').addClass('error'); // set error class to the control group
	            },

	            success: function (label) {
	                label.closest('.control-group').removeClass('error');
	                label.remove();
	            },

	            errorPlacement: function (error, element) {
	                error.addClass('help-small no-left-padding').insertAfter(element.closest('.input-icon'));
	            },

	            submitHandler: function (form) {
	                
					loginProcess();
					
					return false;
	            }
	        });

	        $('.login-form input').keypress(function (e) {
	            if (e.which == 13) {
	                if ($('.login-form').validate().form()) {
	                    loginProcess();
	                }
	                return false;
	            }
	        });

	        $('.forget-form').validate({
	            errorElement: 'label', //default input error message container
	            errorClass: 'help-inline', // default input error message class
	            focusInvalid: false, // do not focus the last invalid input
	            ignore: "",
	            rules: {
	                email: {
	                    required: true,
	                    email: true
	                }
	            },

	            messages: {
	                email: {
	                    required: "Email is required."
	                }
	            },

	            invalidHandler: function (event, validator) { //display error alert on form submit   

	            },

	            highlight: function (element) { // hightlight error inputs
	                $(element)
	                    .closest('.control-group').addClass('error'); // set error class to the control group
	            },

	            success: function (label) {
	                label.closest('.control-group').removeClass('error');
	                label.remove();
	            },

	            errorPlacement: function (error, element) {
	                error.addClass('help-small no-left-padding').insertAfter(element.closest('.input-icon'));
	            },

	            submitHandler: function (form) {
	                window.location.href = "index.html";
	            }
	        });

	        $('.forget-form input').keypress(function (e) {
	            if (e.which == 13) {
	                if ($('.forget-form').validate().form()) {
	                    window.location.href = "index.html";
	                }
	                return false;
	            }
	        });

	        jQuery('#forget-password').click(function () {
	            jQuery('.login-form').hide();
	            jQuery('.forget-form').show();
	        });

	        jQuery('#back-btn').click(function () {
	            jQuery('.login-form').show();
	            jQuery('.forget-form').hide();
	        });
        }

    };

}();